﻿namespace MyTRCP.Common.Models
{
    public class RfStatus : BitWalkableObj
    {
        public byte Rssi { get; set; }
        public byte RssiRr0 { get; set; }
        public byte RssiRr1 { get; set; }
        public byte Noise { get; set; }
        public byte NoiseRr0 { get; set; }
        public byte NoiseRr1 { get; set; }
        public bool RssiR { get; set; }
        public bool NoiseR { get; set; }
        public bool SQ { get; set; }
        public bool SA { get; set; }
        public byte Agc { get; set; }
        public byte I2cErrTimes { get; set; }
        public short R1 { get; set; }
        public bool SpiFlashFail { get; set; }
        public bool AouIsActive { get; set; }
        public bool DspIsConnected { get; set; }
        public byte RpcRxErrTimes { get; set; }
        public bool GpsModulePower { get; set; }
        public bool OriModulePower { get; set; }
        public bool DisMoudlePower { get; set; }
        public short GpsUpdateMinTime { get; set; }
        public byte GpsLockTime { get; set; }
        public byte GpsLastLockTime { get; set; }
        public byte RpcTxErrTimes { get; set; }
        public byte GdiComLost { get; set; }
        public short Rssi2 { get; set; }
        public short Noise2 { get; set; }
        public short EepromWriteTimes { get; set; }
        public RfStatus(byte[] data):base(data)
        {
        }
        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            return bw
                  .WriteBits(Rssi, 8)
                  .WriteBits(RssiRr0, 8)
                  .WriteBits(RssiRr1, 8)
                  .WriteBits(Noise, 8)
                  .WriteBits(NoiseRr0, 8)
                  .WriteBits(NoiseRr1, 8)
                  .WriteBit(RssiR)
                  .WriteBit(NoiseR)
                  .WriteBit(SQ)
                  .WriteBit(SA)
                  .WriteBits(Agc, 4)
                  .WriteBits(I2cErrTimes, 8)
                  .WriteBits(R1, 16)
                  .WriteBit(SpiFlashFail)
                  .WriteBit(AouIsActive)
                  .WriteBit(DspIsConnected)
                  .WriteBits(RpcRxErrTimes, 8)
                  .WriteBits(Agc >> 4, 4)
                  .SkipBits(1)
                  .WriteBit(GpsModulePower)
                  .WriteBit(OriModulePower)
                  .WriteBit(DisMoudlePower)
                  .WriteBits(GpsUpdateMinTime, 13)
                  .WriteBits(GpsLockTime, 8)
                  .WriteBits(GpsLastLockTime, 8)
                  .WriteBits(RpcTxErrTimes, 8)
                  .WriteBits(GdiComLost, 8)
                  .SkipBits(32)
                  .WriteBits(Rssi2, 11)
                  .SkipBits(5)
                  .WriteBits(Noise2, 11)
                  .SkipBits(5)
                  .WriteBits(EepromWriteTimes, 10);
        }

        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            if (bw.ByteLen < 28)
                throw new ArgumentException("字节数组长度过小（28）", nameof(BitWalker));
            Rssi = (byte)bw.ReadBits(8);
            RssiRr0 = (byte)bw.ReadBits(8);
            RssiRr1 = (byte)bw.ReadBits(8);
            Noise = (byte)bw.ReadBits(8);
            NoiseRr0 = (byte)bw.ReadBits(8);
            NoiseRr1 = (byte)bw.ReadBits(8);
            RssiR = bw.ReadBit();
            NoiseR = bw.ReadBit();
            SQ = bw.ReadBit();
            SA = bw.ReadBit();
            Agc = (byte)bw.ReadBits(4);
            I2cErrTimes = (byte)bw.ReadBits(8);
            R1 = (short)bw.ReadBits(16);
            SpiFlashFail = bw.ReadBit();
            AouIsActive = bw.ReadBit();
            DspIsConnected = bw.ReadBit();
            RpcRxErrTimes = (byte)bw.ReadBits(8);
            Agc += (byte)(bw.ReadBits(4) << 4);
            bw.SkipBits(1);
            GpsModulePower = bw.ReadBit();
            OriModulePower = bw.ReadBit();
            DisMoudlePower = bw.ReadBit();
            GpsUpdateMinTime = (short)bw.ReadBits(13);
            GpsLockTime = (byte)bw.ReadBits(8);
            GpsLastLockTime = (byte)bw.ReadBits(8);
            RpcTxErrTimes = (byte)bw.ReadBits(8);
            GdiComLost = (byte)bw.ReadBits(8);
            bw.SkipBits(32);
            Rssi2 = (short)bw.ReadBits(11);
            bw.SkipBits(5);
            Noise2 = (short)bw.ReadBits(11);
            bw.SkipBits(5);
            EepromWriteTimes = (short)bw.ReadBits(10);
            return bw;
        }
    }
}